<div class="row-fluid clearfix">
    <div class="col-md-12 column">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页</a>
            </li>
            <li class="active">
                机构信息管理
            </li>
        </ul>
    </div>
    <div class="col-md-12 column">
        <div class="col-sm-12 div-page">
            <div class="col-sm-12 div-table">
                <div class="col-sm-12 custom-selected">
                    <div>
                        <select class="form-control" id="level1" style="margin-left: -15px;width:136px">
                        </select>
                    </div>
                </div>
                <div id="custom-toolbar">

                </div>
                <table id="table" class="table table-hover table-condensed" data-toolbar="#custom-toolbar"
                       data-click-to-select="true"  data-single-select="true" data-height="430">
                    <thead>
                    <tr>
                        <th class="col-sm-1" data-field="state" data-checkbox="true" ></th>
                        <th class="col-sm-4" data-field="organTreeName" data-halign="left" data-align="left" data-width="320" data-events="operateEvents">机构名称</th>
                        <!--<th data-visible="false" data-field="organName" data-halign="center" data-align="center" >机构名称</th>-->
                        <th class="col-sm-2" data-field="organId" data-halign="center" data-align="center">机构号</th>
                        <th class="col-sm-2" data-field="ABISCode" data-halign="center" data-align="center">ABIS行号</th>
                        <!--<th data-visible="false" data-field="organLevel" data-halign="center" data-align="center">机构级别</th>-->
                        <th class="col-sm-2" data-field="organLevelName" data-halign="center" data-width="80" data-align="center">机构级别</th>
                        <th class="col-sm-1" data-field="organState" data-halign="center" data-align="center" data-width="120" data-formatter="organStateFormatter">机构状态</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <div class="col-sm-12 alert alert-success div-alert" id="events-result" data-es="Aquí se muestra el resultado del evento"></div>
        <!-- #遮罩层 --->
        <div class="modal fade" id="modal-DataEdit" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <div class="modal-title" id="myModalLabel">
                            机构信息管理
                        </div>
                    </div>
                    <form id="validForm" name="validForm" method="post" class="form-horizontal layer_msg">
                        <input type="hidden" data-field="actionType" id="actionType" name="actionType" />
                        <input type="hidden" data-field="ISDEP" id="ISDEP" name="ISDEP" value="0" />
                        <input type="hidden" data-field="SanNong" id="SanNong" name="SanNong" value="0" />
                        <div class="modal-body">
                            <div class="row-fluid">
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">机构ID</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="Id" name="Id" placeholder="系统自动生成"
                                               readonly />
                                    </div>
                                </div>
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">机构名称</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="Name" name="Name" placeholder=""
                                               required  data-fv-notempty-message="请输入" />
                                    </div>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">父机构ID</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="ParentId" name="ParentId" placeholder=""
                                               readonly />
                                    </div>
                                </div>
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">父机构名称</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="ShortName" name="ShortName" placeholder=""
                                               readonly />
                                    </div>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">机构级别</label>
                                    <div class="col-sm-8">
                                        <input type="hidden" id="OrgLevel" name="OrgLevel" />
                                        <input type="text" class="form-control" id="OrgLevelName" name="OrgLevelName" placeholder=""
                                               readonly />
                                    </div>
                                </div>
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">状态</label>
                                    <div class="col-sm-8">
                                        <select class="form-control" id="Disused" name="Disused" required>
                                            <option value="" selected="selected">请选择</option>
                                            <option value="0">启用</option>
                                            <option value="1">停用</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">ABIS号</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="ABISCode" name="ABISCode" placeholder=""
                                               required  data-fv-notempty-message="请输入" />
                                    </div>
                                </div>
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">联系电话</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="Tel" name="Tel" placeholder=""
                                                />
                                    </div>
                                </div>
                            </div>
                            <div class="row-fluid">
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">地址</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="Address" name="Address" placeholder=""
                                                />
                                    </div>
                                </div>
                                <div class="col-sm-6 form-group">
                                    <label class="col-sm-4 control-label">邮编</label>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" id="PostCode" name="PostCode" placeholder=""
                                                />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button id="modal-btn-save" type="submit" class="btn btn-primary">保存</button>
                            <button id="modal-btn-del" type="button" class="btn btn-danger">确认删除</button>
                            <button id="modal-btn-close" type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
function organStateFormatter(value, row) {
    var v='未知';
    value=='0' ? v='启用':v='停用';
    return v;
}
$(function(){
    if($.cookie("OperClass")=="1"){
        FormatSelect(globalUrl+"api/Site?orgid=910",$("#level1"),"请选择","",function(resp){});
        $("#level1").val("01");
        $("#level1").change(function(){
            if($("#level1").val()!="")
            {
                $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Site/"+$("#level1").val()});
            }
        });
    }
    else
    {
        $("#level1").parent().parent().hide();
    }

    //绑定列自定义事件
    window.operateEvents = {
        'click span': function (e, value, row, index) {
            opTree($("#table tbody ").find("tr").eq(index),"");
        }
    };
    /*
     <div class="form-inline" role="form">
     <button id="btn-CommonDataAdd"  type="button" role="button" class="btn btn-sm btn-Data btn-primary disabled">
     <span class="glyphicon glyphicon-plus"></span> 新增</button>
     <button id="btn-CommonDataModify" type="button" role="button" class="btn btn-sm btn-Data btn-success disabled">
     <span class="glyphicon glyphicon-pencil"></span> 修改</button>
     <button id="btn-CommonDataCancel"  type="button" role="button" class="btn btn-sm btn-Data btn-danger disabled" >
     <span class="glyphicon glyphicon-trash"></span> 删除</button>
     <button id="btn-CommonDataDetail" type="button" role="button" class="btn btn-sm btn-Data btn-info disabled">
     <span class="glyphicon glyphicon-th-list"></span> 详情</button>
     </div>
    */

    var btnHtml ;
    var btnRole = JSON.parse($.cookie("Role_note"));

    btnHtml = '<div class="form-inline" role="form">';
    btnHtml += (btnRole["CommonDataAdd"]) ? '<button id="btn-CommonDataAdd"  type="button" role="button" class="btn btn-sm btn-Data btn-primary disabled">' +
            '<span class="glyphicon glyphicon-plus"></span> 新增</button> ' : '' ;
    btnHtml += (btnRole["CommonDataModify"]) ? '<button id="btn-CommonDataModify" type="button" role="button" class="btn btn-sm btn-Data btn-success disabled">' +
            '<span class="glyphicon glyphicon-pencil"></span> 修改</button> ' : '' ;
    btnHtml += (btnRole["CommonDataCancel"]) ? '<button id="btn-CommonDataCancel"  type="button" role="button" class="btn btn-sm btn-Data btn-danger disabled" >' +
            '<span class="glyphicon glyphicon-trash"></span> 删除</button> ' : '' ;

    btnHtml += '<button id="btn-CommonDataDetail"  type="button" role="button" class="btn btn-sm btn-Data btn-info disabled" >' +
            '<span class="glyphicon glyphicon-trash"></span> 详情</button> ' ;
    $("#custom-toolbar").append(btnHtml);

    $('#table').bootstrapTable({
        url: globalUrl+'api/Site/01',//省行管理员：默认显示01机构信息，市、支、网点显示对应信息
        responseHandler:function(resp){
            var dataSet=[];
            GetTreeTableData(dataSet,0,resp.data);
            return dataSet;
        },
        pagination:false,
        pageSize:10,
        search:true
    })
    .on('click-row.bs.table', function (e, row, $element) {
        if ( $element.hasClass('selected') ) {
            $element.removeClass('info');
        }
        else {
            $element.parent('tbody').find(' > tr.selected ').removeClass("info");
            $element.addClass('info');
        }
    })
    .on('dbl-click-row.bs.table', function (e, row, $element) {
    })
    .on('check.bs.table', function (e, row, $element) {
        $(".btn-Data").removeClass("disabled");
        row.organLevel=="4"? $("#btn-CommonDataAdd").addClass("disabled"):$("#btn-CommonDataAdd").removeClass("disabled");
    })
    .on('uncheck.bs.table', function (e, row, $element) {
        $(".btn-Data").addClass("disabled");
    });

    $('#validForm')
            .formValidation({
                message: 'This value is not valid',
                icon: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                }
            })
            .on('success.form.fv', function(e) {
                e.preventDefault();
                var $form = $(e.target);
                var bv = $form.data('formValidation');
                if($('#validForm').find(' input[name="actionType"] ').val()=="edit"){
                    var data = JsonToRow($form);
                    $.ajax({
                        data:data,
                        type: "put",
                        url: globalUrl+"api/Site",
                        dataType: "json",
                        success: function (resp) {
                            if(resp.result)
                            {
                                dataBindToTreeTable($('#table'),$('#validForm'));
                                $('#modal-DataEdit').modal('hide');
                            }
                            else
                            {
                                alert(JSON.stringify(resp));
                            }
                        },
                        error:function(){
                            alert("插入数据失败！");
                        }
                    });
                }else if($('#validForm').find(' input[name="actionType"] ').val()=="add"){
                    var data = {"AddAction":"add","ESI":{}};
                    $("#Id").val($("#ParentId").val());
                    data["ESI"] = JsonToRow($form);
                    $.ajax({
                        data:data,
                        type: "post",
                        url: globalUrl+"api/Site",
                        dataType: "json",
                        success: function (resp) {
                            if(resp.result)
                            {
                                $("#Id").val(resp.data);
                                dataAddToTreeTable($('#table'),$('#validForm'));
                                $('#modal-DataEdit').modal('hide');
                            }
                            else
                            {
                                alert(JSON.stringify(resp));
                            }
                        },
                        error:function(){
                            alert("插入数据失败！");
                        }
                    });
                }else{

                }

            });

});

///表格事件/////////////////////////////////////////////////////
//添加节点，重建树表
function dataAddToTreeTable(_obj,_sourceObj){
    var _row = {};
    var _tableObj = JsonToRow(_sourceObj);

    _row["organId"] = _tableObj["Id"] ;
    _row["ABISCode"] = _tableObj["ABISCode"] ;
    _row["organTreeName"] = '<span></span> ' + _tableObj["Name"] ;
    _row["organName"] = _tableObj["Name"] ;
    _row["organLevel"] = _tableObj["OrgLevel"] ;
    _row["organLevelName"] = _tableObj["OrgLevelName"] ;
    _row["organState"] = _tableObj["Disused"] ;
    _row["_id"] = _obj.find(' tbody > tr ').length;
    _row["_pid"] = _obj.find(' tbody > tr.selected ').attr("id");
    _row["_class"] = GetLevelClass(_tableObj["OrgLevel"]) + ' child ';
    _row["_state"] = 'Expand';
    _row["state"] = false;
    var tableData = _obj.bootstrapTable('getData');
    var dataArray = new Array();
    var dataArrayLeft = new Array();
    var dataArrayRight = new Array();
    var dataArrayMiddle = new Array();
    var index = tableData.length-1;
    //更新父节点显示
    if(_obj.bootstrapTable('getSelections').length>0){
        _obj.bootstrapTable('getSelections')[0]["_class"] = GetLevelClass(_obj.bootstrapTable('getSelections')[0]["organLevel"]) + ' hasChild info ' ;
        index = GetTreeLastNode(_obj.find(' tbody > tr.selected '));
    }

    for(var i=0;i<=index;i++){
        dataArrayLeft[i] = tableData[i];
    }
    for(var i=0;i<tableData.length-parseInt(index)-1;i++){
        dataArrayRight[i] = tableData[i+parseInt(index)+1];
    }
    dataArrayMiddle[0] = _row;
    var dataArray = dataArrayLeft.concat(dataArrayMiddle.concat(dataArrayRight));
    _obj.bootstrapTable("load", dataArray);
}

//刷新行数据
function dataBindToTreeTable(_obj,_sourceObj){
    var _row = {};
    var _index = _obj.find("tr.selected").index();
    var _tableObj = JsonToRow(_sourceObj);
    _row["organId"] = _tableObj["Id"] ;
    _row["ABISCode"] = _tableObj["ABISCode"] ;
    _row["organTreeName"] = '<span></span> ' + _tableObj["Name"] ;
    _row["organName"] = _tableObj["Name"] ;
    _row["organLevel"] = _tableObj["OrgLevel"] ;
    _row["organLevelName"] = _tableObj["OrgLevelName"] ;
    _row["organState"] = _tableObj["Disused"] ;
    _row["_id"] = _obj.find(' tbody > tr ').length;
    _row["_pid"] = _obj.find(' tbody > tr.selected ').attr("id");
    _row["_class"] = GetLevelClass(_tableObj["OrgLevel"]) + ' child ';
    _row["_state"] = 'Expand';
    _row["state"] = false;
    _obj.bootstrapTable('updateRow', {
        index: _index,
        row: _row
    });
}

function GetLevelClass(index){
    var organLevelClass = ['level1','level2','level3','level4'];
    resp = organLevelClass[parseInt(index)-1];
    return resp;
}

//新增按钮
$('#btn-CommonDataAdd').on("click",function () {
    //数据绑定
    var strId = $('#table').bootstrapTable('getSelections')[0]["organId"];
    $.ajax({
        async: true,
        url: globalUrl+"api/Site?Addorgid=" + strId,
        type: 'get',
        dataType: "JSON",
        success: function (resp) {
            var respResult = GetParentOrg(resp);
            dataAjaxBindToModal($('#validForm'),respResult);
            $('#modal-btn-del').addClass('hidden');
            $('#modal-btn-save').removeClass('hidden');
            $('#validForm').find(' input[name="actionType"] ').val("add");
            $('#modal-DataEdit').modal('show');
        },
        error: function (resp) {
            alert("error");
        }
    })
});
//编辑按钮数据传递
$('#btn-CommonDataModify').on("click",function () {
    var strId = $('#table').bootstrapTable('getSelections')[0]["organId"];
    $.ajax({
        async: true,
        url: globalUrl+"api/Site?Addorgid=" + strId,
        type: 'get',
        dataType: "JSON",
        success: function (resp) {
            //{"Id":"01","ParentId":"910","ABISCode":" ","Name":"省分行营业部","ShortName":"江苏省分行","NetNodeArea":null,"Province":null,"OrgLevelName":"市行","OrgLevel":"2","Tel":null,"OrgState":null,"DisusedName":"","Disused":"0","ISDEP":"0","AdminId":null,"SanNong":"0","Address":null,"Landmark":null,"PostCode":null,"Memo":null,"UpdateTime":null,"LongABISCode":null,"SmsReceiverIdint":0}
            //alert(JSON.stringify(resp));
            dataAjaxBindToModal($('#validForm'),resp);
            //$("#events-result").text('Selected values: ' + JSON.stringify($('#table').bootstrapTable('getSelections')));
            $('#modal-btn-del').addClass('hidden');
            $('#modal-btn-save').removeClass('hidden');
            $('#validForm').find(' input[name="actionType"] ').val("edit");
            $('#modal-DataEdit').modal('show');
        },
        error: function (resp) {
            alert("error");
        }
    })
});
//删除按钮
$('#btn-CommonDataCancel').on("click",function () {
    var strId = $('#table').bootstrapTable('getSelections')[0]["organId"];
    $.ajax({
        async: true,
        url: globalUrl+'api/Site?Addorgid=' + strId,
        type: 'get',
        dataType: "JSON",
        success: function (resp) {
            dataAjaxBindToModal($('#validForm'),resp);
            $('#modal-btn-del').removeClass('hidden');
            $('#modal-btn-save').addClass('hidden');
            $('#modal-DataEdit').modal('show');
        },
        error: function (resp) {
            alert("error");
        }
    })

});
//详情按钮
$('#btn-CommonDataDetail').on("click",function () {
    var strId = $('#table').bootstrapTable('getSelections')[0]["organId"];
    $.ajax({
        async: true,
        url: globalUrl+'api/Site?Addorgid=' + strId,
        type: 'get',
        dataType: "JSON",
        success: function (resp) {
            dataAjaxBindToModal($('#validForm'),resp);
            $('#modal-btn-del').addClass('hidden');
            $('#modal-btn-save').addClass('hidden');
            $('#modal-DataEdit').modal('show');
        },
        error: function (resp) {
            alert("error");
        }
    })
});

///弹出层事件/////////////////////////////////////////////////////
//弹出层关闭时重置valid域
$('#modal-DataEdit').on("hide.bs.modal",function () {
    $('#validForm').data('formValidation').resetForm();
    if($('#table').bootstrapTable('getSelections').length==0){
        $(".btn-Data").addClass("disabled");
    }

});
//删除
$('#modal-btn-del').on("click",function () {
    var _date = JsonToRow($('#validForm'));
    $.ajax({
        type: "delete",
        data: _date,
        url: globalUrl+"api/Site",
        dataType: "json",
        success: function (resp) {
            if(resp.result)
            {
                $("#Disused").val("1");
                dataBindToTreeTable($('#table'),$('#validForm'));
                //关闭弹出窗口
                $('#modal-DataEdit').modal('hide');
            }
            else
            {
                alert(resp.msg);
            }
        },
        error:function(){
            alert("更新数据失败！");
        }
    });
});
//弹出层关闭按钮事件
$('#modal-btn-close').on("click",function () {
});

</script>

<!-- /自定义脚本 -->



